package com.gl.emms.whale.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.util.Alarm;
import com.gl.emms.whale.util.Alarms;
import com.gl.emms.whale.util.SystemMsgEntity;
import com.gl.emms.whale.util.TimingControlEntity;

public class ValveAutoControlActivity extends WhaleMonitorActivity implements
OnItemClickListener,TimePickerDialog.OnTimeSetListener{
	private ListView mAlarmsList;
	private Cursor mCursor;
	private LayoutInflater mFactory;
	private LinearLayout mBglinearlayout;
	private int     mHour;
	private int     mMinutes;
	private List<TimingControlEntity> timingControlEntities  = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		//setContentView(R.layout.activity_valveautocontrol);

		//mCursor = Alarms.getAlarmsCursor(getContentResolver());

		updateLayout();


	}

	private void updateLayout() {
		setContentView(R.layout.alarm_clock);
		mBglinearlayout = (LinearLayout) findViewById(R.id.base_layout);
		mAlarmsList = (ListView) findViewById(R.id.alarms_list);
		/*
        AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
        mAlarmsList.setAdapter(adapter);
		 */
		timingControlEntities = new ArrayList<TimingControlEntity>();
		TimingControlEntity timingControlEntity1 = new TimingControlEntity(1, "11", "1", false);
		TimingControlEntity timingControlEntity2 = new TimingControlEntity(2, "11", "1", false);
		TimingControlEntity timingControlEntity3 = new TimingControlEntity(3, "11", "1", false);
		TimingControlEntity timingControlEntity4 = new TimingControlEntity(4, "11", "1", false);
		timingControlEntities.add(timingControlEntity1);
		timingControlEntities.add(timingControlEntity2);
		timingControlEntities.add(timingControlEntity3);
		timingControlEntities.add(timingControlEntity4);
		TimingControlAdapter adapter = new TimingControlAdapter(this, timingControlEntities);
		mAlarmsList.setAdapter(adapter);
		mAlarmsList.setVerticalScrollBarEnabled(true);
		//mAlarmsList.setOnItemClickListener(this);
		mAlarmsList.setOnCreateContextMenuListener(this);
		/*
        View addAlarm = findViewById(R.id.add_alarm);
        addAlarm.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    addNewAlarm();
                }
            });
        // Make the entire view selected when focused.
        addAlarm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    v.setSelected(hasFocus);
                }
        });
		 */
	}
	private void addNewAlarm() {
		startActivity(new Intent(this, SetAlarm.class));
	}
	private class TimingControlAdapter extends BaseAdapter {
		private ValveAutoControlActivity context;
		private List<TimingControlEntity> list;// 消息对象数组
		private LayoutInflater inflater;
		public TimingControlAdapter(ValveAutoControlActivity context, List<TimingControlEntity> list) {
			this.context = context;
			this.list = list;
			inflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return list.size();
		}

		public Object getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder viewHolder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_controlswitch, null);
				viewHolder = new ViewHolder();
				viewHolder.indicator = convertView.findViewById(R.id.indicator);

	            // Set the initial resource for the bar image.
	            final ImageView barOnOff =
	                    (ImageView) convertView.findViewById(R.id.bar_onoff);
	            // Set the initial state of the clock "checkbox"
	            final CheckBox clockOnOff =
	                    (CheckBox) convertView.findViewById(R.id.clock_onoff);

	            // Clicking outside the "checkbox" should also change the state.
	            //对checkbox设置监听，使里外一致
	            viewHolder.indicator.setOnClickListener(new OnClickListener() {
	                    public void onClick(View v) {
	                        clockOnOff.toggle();
	                        controlSwitch(clockOnOff.isChecked(),barOnOff);
	                    }
	            });

				viewHolder.time = (TextView) convertView
						.findViewById(R.id.timeDisplay);

				viewHolder.time.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						showTimePicker();
					}
				});
				viewHolder.type = (TextView) convertView
						.findViewById(R.id.controltype);
				viewHolder.type.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						showControlTypeChoose();
					}
				});
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			if(list.isEmpty())
			{

			}else
			{
				TimingControlEntity entity = list.get(position);
				viewHolder.time.setText("dd");
				viewHolder.type.setText("ee");
			}


			return convertView;
		}

		class ViewHolder {
			public View indicator;
			public TextView time;
			public TextView type;


		}


	};
	private void controlSwitch(boolean enabled,ImageView bar) {
		bar.setImageResource(enabled ? R.drawable.ic_indicator_on
				: R.drawable.ic_indicator_off);
		/*
        Alarms.enableAlarm(this, alarm.id, enabled);
        if (enabled) {
            SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
                    alarm.daysOfWeek);
        }
		 */
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		showTimePicker();

	}
	private void showTimePicker() {
		new TimePickerDialog(this, this, mHour, mMinutes,
				DateFormat.is24HourFormat(this)).show();
	}
	private void showControlTypeChoose() {
		CharSequence[] items = { "强制开", "强制关", "强制撤销" };
		AlertDialog imageDialog = new AlertDialog.Builder(this)
		.setTitle("选择控制方式")
		.setIcon(android.R.drawable.btn_star)
		.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				// 强制开
				if (item == 0) {
				}
				//强制关 
				else if (item == 1) {
				}
				//强制撤销
				else if (item == 2) {
				}
			}
		}).create();
		imageDialog.show();
	}

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		// onTimeSet is called when the user clicks "Set"
		mHour = hourOfDay;
		mMinutes = minute;
		// If the time has been changed, enable the alarm.
		// Save the alarm and pop a toast.
	}
}